package stackAndQueue;

import java.util.Stack;

/**
 * 150. 逆波兰表达式求值
 */
public class N150_evalRPN {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (String token : tokens) {
            if (token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/")) {
                int num1 = stack.pop();
                int num2 = stack.pop();
                if (token.equals("+")) stack.push(num2 + num1);
                if (token.equals("-")) stack.push(num2 - num1);
                if (token.equals("*")) stack.push(num2 * num1);
                if (token.equals("/")) stack.push(num2 / num1);
            } else {
                stack.push(Integer.parseInt(token));
            }
        }
        return stack.pop();
    }

    public static void main(String[] args) {
        N150_evalRPN solution = new N150_evalRPN();
//        String[] tokens = {"2", "1", "+", "3", "*"};
//        String[] tokens = {"4", "13", "5", "/", "+"};
        String[] tokens = {"10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"};

        System.out.println(solution.evalRPN(tokens));
    }

}
